package pzl.com.drawrelated.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author zl.peng
 * @version [1.0, 2016-11-21]
 */
public class V_RadialGradient extends View {
    int w;
    int h;
    Paint paint;
    RadialGradient radialGradient;

    public V_RadialGradient(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(1);
    }

    /**
     * 三个TileMode可选CLAMP、REPEAT、MIRROR
     */

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        w = getWidth();
        h = getHeight();
        radialGradient = new RadialGradient(w / 2, 200 / 2, 100, new int[]{Color.parseColor("#f2312e"), Color.parseColor("#2ef2cb"), Color.parseColor("#d15c329f"), Color.parseColor("#e6663b")}, null, Shader.TileMode.CLAMP);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        paint.setShader(radialGradient);
        Rect rect1 = new Rect();
        rect1.top = 20;
        rect1.left = 20;
        rect1.right = w - 20;
        rect1.bottom = rect1.top + 400;
        canvas.drawRect(rect1, paint);

    }

}
